﻿
@{
    ViewBag.Title = "UserEdit";
    Layout = "~/Areas/Admin/Views/Shared/_Layout.cshtml";
    var user = ViewBag.User as LuDragon.Data.Entities.Sys_User;
    var roleList = ViewBag.RoleList as List<LuDragon.Data.Entities.Sys_Role>;
}


<span class="layui-breadcrumb">
    <a>系统设置</a>
    <a>用户管理</a>
    <a><cite>编辑</cite></a>
</span>
<hr />
<form class="layui-form" method="post" action="Save" id="form1">
    <table class="layui-table layui-table-add layui-table-detail" lay-skin="nob">
        <tbody>
            <tr>
                <th><span class="red">*</span>所属角色</th>
                <td>
                    <div class="width">
                        <select lay-verify="" id="role" name="RoleId">
                            @for (int i = 0; i < roleList.Count; i++)
                            {
                                <option value="@roleList[i].RoleId">@roleList[i].RoleName</option>
                            }
                        </select>
                    </div>
                </td>
            </tr>
            <tr>
                <th><span class="red">*</span>用户名</th>
                <td>
                    <div class="width">
                        <input type="text" name="UserName" placeholder="请输入用户名" autocomplete="off" class="layui-input" value="@(user?.UserName)">
                    </div>
                </td>
            </tr>
            <tr>
                <th><span class="red">*</span>密码</th>
                <td>
                    <div class="width">
                        <input type="password" name="Password" placeholder="请输入密码" autocomplete="off" class="layui-input" value="@(user?.Password)"  id="pwd">
                    </div>
                </td>
            </tr>
            <tr>
                <th><span class="red"></span>姓名</th>
                <td>
                    <div class="width">
                        <input type="text" name="CnName" placeholder="请输入姓名" autocomplete="off" class="layui-input" value="@(user?.CnName)">
                    </div>
                </td>
            </tr>
            <tr>
                <th><span class="red"></span>性别</th>
                <td>
                    <div class="width">
                        <input type="radio" name="Sex" id="men" value="男" title="男" checked >
                        <input type="radio" name="Sex" id="women" value="女" title="女">
                    </div>
                </td>
            </tr>
            <tr>
                <th><span class="red"></span>电话</th>
                <td>
                    <div class="width">
                        <input type="text" name="Phone" placeholder="请输入电话" autocomplete="off" class="layui-input" value="@(user?.Phone)">
                    </div>
                </td>
            </tr>
            <tr>
                <th><span class="red"></span>邮箱</th>
                <td>
                    <div class="width">
                        <input type="text" name="Email" placeholder="请输入邮箱" autocomplete="off" class="layui-input" value="@(user?.Email)">
                    </div>
                </td>
            </tr>
            <tr>
                <th><span class="red"></span>是否启用</th>
                <td>
                    <div class="width">
                        <input type="checkbox" checked="@(user?.Enabled)" lay-skin="switch" lay-text="是|否" id="check">
                    </div>
                </td>
            </tr>
        </tbody>
    </table>
    <div class="layui-button">
        <button class="layui-btn" id="save">保存</button>
        <button type="reset" class="layui-btn layui-btn-primary" onclick="javascript: history.back();">返回上一页</button>
    </div>
    <input type="hidden" name="UserId" value="@(user?.UserId)" />
    <input type="hidden" name="IsSystem" value="@(user?.IsSystem)" />
    <input type="hidden" name="RegisterTime" value="@(user?.RegisterTime)" />
    <input type="hidden" name="Enabled" id="Enabled" value="false"/>
</form>

<script src="~/Content/js/jquery.md5.js"></script>
<script>
    var user ="@(user?.UserId)";
    if (user) {
        //设置密码不可编辑
        $("#pwd").attr("disabled", "disabled");
    }
    //角色列表
    var roleId = "@(user?.RoleId)";
    $("#role").val(roleId);
    //性别 默认男
    var sex = "@(user?.Sex)";
    if (sex == "女") {
        $("#men").removeAttr('checked');
        $("#women").attr('checked','checked');
    }
    
    //保存
    $("#save").click(function () {
        //加密
        if (!$("#pwd").is(":disabled")) {
            $("#pwd").val($.md5($("#pwd").val()));
        }
        $("#pwd").removeAttr("disabled");//删除disabled属性 表单上传
        $("#Enabled").val($("#check").is(":checked") ? true : false);//启用
        $("#form1").ajaxForm(function (result) {
            var data = JSON.parse(result);
            if (data.Code == 200) {
                dialog.prompt("保存成功");
                location.href = "UserList";
            } else {
                dialog.prompt("操作失败，请稍后重试");
            }
        });
    });
</script>